﻿using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace Profit.Controls
{
    /// <summary>
    /// Interaction logic for GaugeUsageControl.xaml
    /// </summary>
    public partial class GaugeUsageControl : UserControl
    {
        public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(GaugeUsageControl), new PropertyMetadata((double)0, OnValueChanged));
        public static DependencyProperty FillBrushProperty = DependencyProperty.Register("FillBrush", typeof(Brush), typeof(GaugeUsageControl), new PropertyMetadata(Brushes.Green));
        public static DependencyProperty OverflowProperty = DependencyProperty.Register("Overflow", typeof(bool), typeof(GaugeUsageControl), new PropertyMetadata(false));
        public static DependencyProperty OverflowBrushProperty = DependencyProperty.Register("OverflowBrush", typeof(Brush), typeof(GaugeUsageControl), new PropertyMetadata(Brushes.Red));
        public static DependencyProperty NormalBrushProperty = DependencyProperty.Register("NormalBrush", typeof(Brush), typeof(GaugeUsageControl), new PropertyMetadata(Brushes.Green));
        
        public GaugeUsageControl()
        {
            InitializeComponent();
        }

        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public Brush FillBrush
        {
            get { return (Brush)GetValue(FillBrushProperty); }
            set { SetValue(FillBrushProperty,value); }
        }

        public Brush NormalBrush
        {
            get { return (Brush)GetValue(NormalBrushProperty); }
            set { SetValue(NormalBrushProperty, value); }
        }

        public Brush OverflowBrush
        {
            get { return (Brush)GetValue(OverflowBrushProperty); }
            set { SetValue(OverflowBrushProperty, value); }
        }

        public bool Overflow
        {
            get { return (bool)GetValue(OverflowProperty); }
            set { SetValue(OverflowProperty,value); }
        }

        private static void OnValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            o.SetValue(OverflowProperty, e.NewValue!=null && (double) e.NewValue > 100);
            o.SetValue(FillBrushProperty, (bool)o.GetValue(OverflowProperty) ? o.GetValue(OverflowBrushProperty) : o.GetValue(NormalBrushProperty));
        }
    }
}
